package com.sdhs.analyze.service.monitor;


import com.sdhs.analyze.bean.DicNodeType;
import com.sdhs.analyze.bean.InfoGantryStatusEnvironment;
import com.sdhs.analyze.bean.InfoNodeStatus;
import com.sdhs.analyze.bean.device.CabientInfo;
import com.sdhs.analyze.bean.device.FhAirCondition;
import com.sdhs.analyze.bean.device.FhAlarm;
import com.sdhs.analyze.bean.device.FhBattery;
import com.sdhs.analyze.bean.device.FhPowerInfo;
import com.sdhs.analyze.bean.device.FhTempHum;
import com.sdhs.analyze.bean.device.GtAirCondition;
import com.sdhs.analyze.bean.device.GtAlarmInfo;
import com.sdhs.analyze.bean.device.GtBattery;
import com.sdhs.analyze.bean.device.GtPowerInfo;
import com.sdhs.analyze.bean.device.GtTempHum;
import com.sdhs.analyze.bean.device.HwAirCondition;
import com.sdhs.analyze.bean.device.HwAlarm;
import com.sdhs.analyze.bean.device.HwBattery;
import com.sdhs.analyze.bean.device.HwPowerSupply;
import com.sdhs.analyze.bean.device.HwTempHumidity;
import com.sdhs.analyze.bean.monitorinfo.GantryMonitorInfo;
import com.sdhs.analyze.bean.org.OrgInfo;
import com.sdhs.analyze.common.BizConstants;
import com.sdhs.analyze.dao.device.DeviceMapper;
import com.sdhs.analyze.utils.StatusCheckUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class EnvironmentService {
    private static Logger logger = LoggerFactory.getLogger(EnvironmentService.class);

    @Autowired
    private NodeStatusService nodeStatusService;

    @Autowired
    private DeviceMapper deviceMapper;


    private String split = "|";

    /**
     * 动环监测
     *
     * @param
     * @param nodeStatusMap
     * @param orgInfo
     * @param nodeTypeMap
     * @return
     */
    public void environmentMonitor(
            OrgInfo orgInfo,
            Map<String, InfoNodeStatus> nodeStatusMap,
            Map<String, DicNodeType> nodeTypeMap,
            InfoNodeStatus parentNodeStatus,
            GantryMonitorInfo gantryMonitorInfo) {

        DicNodeType environmentNodeType = nodeTypeMap.get(BizConstants.NODE_TYPE_ENVIRONMENT);
        DicNodeType powerNodeType = nodeTypeMap.get(BizConstants.NODE_TYPE_ENVIRONMENT_POWER);
        DicNodeType batteryLevelNodeType = nodeTypeMap.get(BizConstants.NODE_TYPE_ENVIRONMENT_BATTERY_LEVEL);
        DicNodeType viltageNodeType = nodeTypeMap.get(BizConstants.NODE_TYPE_ENVIRONMENT_VOLTAGE);
        DicNodeType electricCurrentNodeType = nodeTypeMap.get(BizConstants.NODE_TYPE_ENVIRONMENT_ELECTRIC_CURRENT);
        DicNodeType tempNodeType = nodeTypeMap.get(BizConstants.NODE_TYPE_ENVIRONMENT_TEMP);
        DicNodeType humidityNodeType = nodeTypeMap.get(BizConstants.NODE_TYPE_ENVIRONMENT_HUMIDITY);
        DicNodeType smogNodeType = nodeTypeMap.get(BizConstants.NODE_TYPE_ENVIRONMENT_SMOG);
        DicNodeType waterNodeType = nodeTypeMap.get(BizConstants.NODE_TYPE_ENVIRONMENT_WATER);
        DicNodeType airNodeType = nodeTypeMap.get(BizConstants.NODE_TYPE_ENVIRONMENT_AIR);
        // 检测心跳数据-核心
        InfoGantryStatusEnvironment environment = getEnvironment(orgInfo.getOrgId());

        if (environment == null) {
            gantryMonitorInfo.getEnvironment().setStatus(BizConstants.NODE_STATUS_UNKNOWN);
            return;
        }
        /* 初始化动环节点状态 */
        String nodeName = orgInfo.getOrgName() + split + environmentNodeType.getNodeTypeName();
        InfoNodeStatus environmentNodeStatus = nodeStatusService.makeNodeStatus(
                environmentNodeType, orgInfo.getOrgId(),
                nodeName,
                orgInfo.getOrgId(), BizConstants.NODE_STATUS_UNKNOWN, orgInfo.getOrgId(), "", new Date(0));

        Date checkTime = environment.getCheckTime();

        /* 供电模式 */
        InfoNodeStatus powerModeNodeStatus = StatusCheckUtils.checkByRange(
                environment.getPowerSupplyModeRange(),
                checkTime, powerNodeType,
                orgInfo.getOrgId(), nodeName + split + powerNodeType.getNodeTypeName(),
                orgInfo.getOrgId(), environmentNodeStatus);
        nodeStatusMap.put(powerModeNodeStatus.getKey(), powerModeNodeStatus);

        /* 电池电量 */
        InfoNodeStatus batteryLevelNodeStatus = StatusCheckUtils.checkStatusByThreshold(
                environment.getBatteryLevel(),
                checkTime, batteryLevelNodeType,
                orgInfo.getOrgId(), nodeName + split + batteryLevelNodeType.getNodeTypeName(),
                orgInfo.getOrgId(), environmentNodeStatus);
        nodeStatusMap.put(batteryLevelNodeStatus.getKey(), batteryLevelNodeStatus);

        /* 电压 */
        InfoNodeStatus viltageNodeStatus = StatusCheckUtils.checkStatusByThreshold(
                environment.getVoltage(),
                checkTime, viltageNodeType,
                orgInfo.getOrgId(), nodeName + split + viltageNodeType.getNodeTypeName(),
                orgInfo.getOrgId(), environmentNodeStatus);
        nodeStatusMap.put(viltageNodeStatus.getKey(), viltageNodeStatus);

        /* 电流 */
        InfoNodeStatus electricCurrentNodeStatus = StatusCheckUtils.checkStatusByThreshold(
                environment.getElectricCurrent(),
                checkTime, electricCurrentNodeType,
                orgInfo.getOrgId(), nodeName + split + electricCurrentNodeType.getNodeTypeName(),
                orgInfo.getOrgId(), environmentNodeStatus);
        nodeStatusMap.put(electricCurrentNodeStatus.getKey(), electricCurrentNodeStatus);

        /* 空调 */
        InfoNodeStatus airNodeStatus = StatusCheckUtils.checkByRange(
                environment.getAirWorkStatus(),
                checkTime, airNodeType,
                orgInfo.getOrgId(), nodeName + split + airNodeType.getNodeTypeName(),
                orgInfo.getOrgId(), environmentNodeStatus);
        nodeStatusMap.put(airNodeStatus.getKey(), airNodeStatus);

        /* 温度 */
        InfoNodeStatus tempNodeStatus = StatusCheckUtils.checkStatusByThreshold(
                environment.getTemperature(),
                checkTime, tempNodeType,
                orgInfo.getOrgId(), nodeName + split + tempNodeType.getNodeTypeName(),
                orgInfo.getOrgId(), environmentNodeStatus);
        nodeStatusMap.put(tempNodeStatus.getKey(), tempNodeStatus);

        /* 湿度 */
        InfoNodeStatus humidityNodeStatus = StatusCheckUtils.checkStatusByThreshold(
                environment.getHumidity(),
                checkTime, humidityNodeType,
                orgInfo.getOrgId(), nodeName + split + humidityNodeType.getNodeTypeName(),
                orgInfo.getOrgId(), environmentNodeStatus);
        nodeStatusMap.put(humidityNodeStatus.getKey(), humidityNodeStatus);

        /* 烟雾 */
        InfoNodeStatus smogNodeStatus = StatusCheckUtils.checkByRange(
                environment.getSmogStatus(),
                checkTime, smogNodeType,
                orgInfo.getOrgId(), nodeName + split + smogNodeType.getNodeTypeName(),
                orgInfo.getOrgId(), environmentNodeStatus);
        nodeStatusMap.put(smogNodeStatus.getKey(), smogNodeStatus);

        /* 水浸 */
        InfoNodeStatus waterNodeStatus = StatusCheckUtils.checkByRange(
                environment.getWaterLoggingStatus(),
                checkTime, waterNodeType,
                orgInfo.getOrgId(), nodeName + split + waterNodeType.getNodeTypeName(),
                orgInfo.getOrgId(), environmentNodeStatus);
        nodeStatusMap.put(waterNodeStatus.getKey(), waterNodeStatus);

        /* 保存动环状态 */
        nodeStatusMap.put(environmentNodeStatus.getKey(), environmentNodeStatus);

        gantryMonitorInfo.getEnvironment().setStatus(environmentNodeStatus.getStatus());
        gantryMonitorInfo.getEnvironment().setRemark(String.format("%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d|%d",
                environment.getPowerSupplyMode(),
                powerModeNodeStatus.getStatus(),
                environment.getBatteryLevel() == BizConstants.UNKNOWN_VALUE ? 0 : environment.getBatteryLevel(),
                batteryLevelNodeStatus.getStatus(),
                environment.getVoltage(),
                viltageNodeStatus.getStatus(),
                environment.getElectricCurrent(),
                electricCurrentNodeStatus.getStatus(),
                environment.getTemperature() == BizConstants.UNKNOWN_VALUE ? 0 : environment.getTemperature(),
                tempNodeStatus.getStatus(),
                environment.getHumidity() == BizConstants.UNKNOWN_VALUE ? 0 : environment.getHumidity(),
                humidityNodeStatus.getStatus(),
                environment.getSmogStatus(),
                smogNodeStatus.getStatus(),
                environment.getWaterLoggingStatus(),
                waterNodeStatus.getStatus(),
                environment.getAirWorkStatus(),
                airNodeStatus.getStatus(),
                environment.getAirTemp(),
                environment.getAirWorkMode()
        ));

        /* 判断父节点的状态 */
        StatusCheckUtils.checkParentNodeStatus(environmentNodeStatus.getStatus(),
                environmentNodeStatus.getStatusCheckTime(),
                parentNodeStatus);
    }

    /**
     * @param gantryId
     * @return
     */
    private InfoGantryStatusEnvironment getEnvironment(String gantryId) {
        InfoGantryStatusEnvironment environment = null;
        CabientInfo cabientInfo = deviceMapper.getCabinetByNodeId(gantryId);
        if (cabientInfo == null) {
            return null;
        }
        String deviceBrandCode = cabientInfo.getDeviceBrandCode();
        if (StringUtils.isNotBlank(deviceBrandCode)) {
            // 电源
            if ("01".equals(deviceBrandCode)) {
                environment = new InfoGantryStatusEnvironment();
                HwPowerSupply hwPower = deviceMapper.getHwPower(gantryId);
                HwBattery hwBatteries = deviceMapper.getHwBattery(gantryId);
                HwAirCondition hwAirCondition = deviceMapper.getHwAirCondition(gantryId);
                HwTempHumidity hwTempHum = deviceMapper.getHwTempHum(gantryId);
                HwAlarm hwAlarm = deviceMapper.getHwAlarm(gantryId);

                environment.setCheckTime(new Date());

                if (hwPower != null) {
                    /* 判断供电模式 */
                    Integer hwAcInputOperStatus = hwPower.getHwAcInputOperStatus();
                    if (hwAcInputOperStatus == null || hwAcInputOperStatus == 255) {
                        environment.setPowerSupplyModeRange(BizConstants.STATUS_RANGE_UNKNOWN);
                        environment.setPowerSupplyMode(-2);
                    } else if (hwAcInputOperStatus == 2 || hwAcInputOperStatus == 3) {
                        environment.setPowerSupplyModeRange(BizConstants.STATUS_RANGE_2);
                        environment.setPowerSupplyMode(2);
                    } else {
                        environment.setPowerSupplyModeRange(BizConstants.STATUS_RANGE_1);
                        environment.setPowerSupplyMode(1);
                    }
                    /* 电压 */
                    if (hwPower.getHwApOrAblVoltage() == null || "2147483647".equals(hwPower.getHwApOrAblVoltage())) {
                        environment.setVoltage(BizConstants.UNKNOWN_VALUE);
                    } else {
                        environment.setVoltage(Integer.parseInt(hwPower.getHwApOrAblVoltage()) / 10);
                    }
                    /* 电流 */
                    if (hwPower.getHwAphaseCurrent() == null) {
                        environment.setElectricCurrent(BizConstants.UNKNOWN_VALUE);
                    } else {
                        environment.setElectricCurrent(Integer.parseInt(hwPower.getHwAphaseCurrent()));
                    }
                    environment.setCheckTime(hwPower.getCheckTime());
                } else {
                    environment.setPowerSupplyModeRange(BizConstants.STATUS_RANGE_UNKNOWN);
                    environment.setPowerSupplyMode(-2);
                    /* 电压 */
                    environment.setVoltage(BizConstants.UNKNOWN_VALUE);
                    /* 电流 */
                    environment.setElectricCurrent(BizConstants.UNKNOWN_VALUE);
                }

                if (hwBatteries != null) {
                    try {
                        /* 电池状态 */
                        Integer hwAcbGroupBatRunningState = hwBatteries.getHwAcbGroupBatRunningState();
                        Float hwAcbGroupTotalRemainCapacity = hwBatteries.getHwAcbGroupTotalRemainCapacity();
                        if (hwAcbGroupBatRunningState == null || hwAcbGroupBatRunningState == 255) {
                            environment.setBatteryStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                        } else if (hwAcbGroupBatRunningState == 2) {
                            environment.setBatteryStatus(BizConstants.STATUS_RANGE_2);
                        } else {
                            environment.setBatteryStatus(BizConstants.STATUS_RANGE_1);
                        }
                        if (hwAcbGroupTotalRemainCapacity != null) {
                            environment.setBatteryLevel((int) (hwAcbGroupTotalRemainCapacity / 10));
                        } else {
                            environment.setBatteryLevel(BizConstants.UNKNOWN_VALUE);
                        }
                        environment.setCheckTime(hwBatteries.getCheckTime());
                    } catch (Exception ex) {
                        logger.error(ex.getMessage(), ex);
                        environment.setBatteryStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                        environment.setBatteryLevel(BizConstants.UNKNOWN_VALUE);
                    }
                } else {
                    environment.setBatteryStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    environment.setBatteryLevel(BizConstants.UNKNOWN_VALUE);
                }

                if (hwAirCondition != null) {
                    /* 空调 */
                    Integer hwDcAirRunStatus = hwAirCondition.getHwDcAirRunStatus();
                    if (hwDcAirRunStatus == null) {
                        environment.setAirWorkStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    } else if (hwDcAirRunStatus == 1 || hwDcAirRunStatus == 2) {
                        environment.setAirWorkStatus(BizConstants.STATUS_RANGE_1);
                    } else if (hwDcAirRunStatus == 3) {
                        environment.setAirWorkStatus(BizConstants.STATUS_RANGE_2);
                    } else {
                        environment.setAirWorkStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    }
                    if (hwAirCondition.getHwDcAirEnterChannelTemp() != null) {
                        environment.setAirTemp(Integer.valueOf(hwAirCondition.getHwDcAirEnterChannelTemp()));
                    } else {
                        environment.setAirTemp(BizConstants.UNKNOWN_VALUE);
                    }
                    if (hwAirCondition.getHwCoolingDevicesMode() != null) {
                        environment.setAirWorkMode(Integer.valueOf(hwAirCondition.getHwCoolingDevicesMode()));
                    } else {
                        environment.setAirWorkMode(-2);
                    }
                    environment.setCheckTime(hwAirCondition.getCheckTime());
                } else {
                    environment.setAirWorkStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    environment.setAirTemp(BizConstants.UNKNOWN_VALUE);
                }

                if (hwTempHum != null) {
                    try {
                        /* 温度 */
                        if (hwTempHum.getHwTemHumEquipAmbientTemp() == null) {
                            environment.setTemperature(BizConstants.UNKNOWN_VALUE);
                        } else {
                            environment.setTemperature(hwTempHum.getHwTemHumEquipAmbientTemp() / 10);
                        }

                        /* 湿度 */
                        if (hwTempHum.getHwTemHumEquipAmbientHumidity() == null) {
                            environment.setHumidity(BizConstants.UNKNOWN_VALUE);
                        } else {
                            environment.setHumidity(hwTempHum.getHwTemHumEquipAmbientHumidity() / 10);
                        }

                        environment.setCheckTime(hwTempHum.getCheckTime());
                    } catch (Exception ex) {
                        /* 温度 */
                        environment.setTemperature(BizConstants.UNKNOWN_VALUE);
                        /* 湿度 */
                        environment.setHumidity(BizConstants.UNKNOWN_VALUE);
                    }
                } else {
                    /* 温度 */
                    environment.setTemperature(BizConstants.UNKNOWN_VALUE);
                    /* 湿度 */
                    environment.setHumidity(BizConstants.UNKNOWN_VALUE);
                }

                if (hwAlarm != null) {
                    /* 烟感 */
                    if (hwAlarm.getHwSmokeSensorStatus() == null) {
                        environment.setSmogStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    } else if (hwAlarm.getHwSmokeSensorStatus() == 0) {
                        environment.setSmogStatus(BizConstants.STATUS_RANGE_1);
                    } else if (hwAlarm.getHwSmokeSensorStatus() == 1) {
                        environment.setSmogStatus(BizConstants.STATUS_RANGE_2);
                    } else {
                        environment.setSmogStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    }
                    /* 水浸 */
                    if (hwAlarm.getHwWaterSensorStatus() == null) {
                        environment.setWaterLoggingStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    } else if (hwAlarm.getHwWaterSensorStatus() == 0) {
                        environment.setWaterLoggingStatus(BizConstants.STATUS_RANGE_1);
                    } else if (hwAlarm.getHwWaterSensorStatus() == 1) {
                        environment.setWaterLoggingStatus(BizConstants.STATUS_RANGE_2);
                    } else {
                        environment.setWaterLoggingStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    }
                    environment.setCheckTime(hwAlarm.getCheckTime());
                } else {
                    /* 烟感 */
                    environment.setSmogStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    /* 水浸 */
                    environment.setWaterLoggingStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                }
            } else if ("02".equals(deviceBrandCode)) {
                GtPowerInfo gtPower = deviceMapper.getGtPower(gantryId);
                List<GtBattery> gtBatteryList = deviceMapper.getGtBattery(gantryId);
                GtTempHum gtTempHum = deviceMapper.getGtTempHum(gantryId);
                List<GtAirCondition> gtAirConditionList = deviceMapper.getGtAirCondition(gantryId);
                GtAlarmInfo gtAlarmInfo = deviceMapper.getGtAlarmInfo(gantryId);
                environment = new InfoGantryStatusEnvironment();

                if (gtPower != null) {
                    /* 输入电压 */
                    String voltage = gtPower.getGtPowerVoltageIn();
                    int volt = 0;
                    try {
                        if (StringUtils.isNotBlank(voltage)) {
                            Double voltageDouble = Double.parseDouble(voltage);
                            volt = voltageDouble.intValue();
                            environment.setVoltage(volt);
                        } else {
                            environment.setVoltage(BizConstants.UNKNOWN_VALUE);
                        }
                    } catch (Exception ex) {
                        environment.setVoltage(BizConstants.UNKNOWN_VALUE);
                    }
                    /* 判断供电模式 */
                    Integer powerModel = gtPower.getGtPowerModel();
                    if (powerModel == null) {
                        environment.setPowerSupplyModeRange(BizConstants.STATUS_RANGE_UNKNOWN);
                        environment.setPowerSupplyMode(-2);
                    } else if (powerModel == 3 || (powerModel == 2 && volt > 180)) {
                        environment.setPowerSupplyModeRange(BizConstants.STATUS_RANGE_1);
                        environment.setPowerSupplyMode(1);
                    } else if (powerModel == 2 && volt < 180) {
                        environment.setPowerSupplyModeRange(BizConstants.STATUS_RANGE_2);
                        environment.setPowerSupplyMode(2);
                    } else if (powerModel == 0) {
                        environment.setPowerSupplyModeRange(BizConstants.STATUS_RANGE_2);
                        environment.setPowerSupplyMode(3);
                    } else {
                        environment.setPowerSupplyModeRange(BizConstants.STATUS_RANGE_UNKNOWN);
                        environment.setPowerSupplyMode(-2);
                    }

                    /* 电流 */
                    String electricCurrent = gtPower.getGtPowerElectricCurrent();
                    if (electricCurrent != null) {
                        try {
                            Double electricCurrentDouble = Double.parseDouble(electricCurrent);
                            environment.setElectricCurrent(electricCurrentDouble.intValue());
                        } catch (Exception ex) {
                            environment.setElectricCurrent(BizConstants.UNKNOWN_VALUE);
                        }
                    } else {
                        environment.setElectricCurrent(BizConstants.UNKNOWN_VALUE);
                    }

                } else {
                    environment.setPowerSupplyModeRange(BizConstants.STATUS_RANGE_UNKNOWN);
                    environment.setPowerSupplyMode(-2);
                    environment.setVoltage(BizConstants.UNKNOWN_VALUE);
                    environment.setElectricCurrent(BizConstants.UNKNOWN_VALUE);
                }

                if (gtBatteryList != null && gtBatteryList.size() > 0) {
                    Integer batteryLevel = BizConstants.UNKNOWN_VALUE;
                    /* 电池电量 */
                    GtBattery gtBattery = gtBatteryList.get(0);
                    /* 如电池容量未获取到且剩余百分比为0，视为信息异常 */
                    if ((gtBattery.getGtAcbGroupTotalCapacity() == null || gtBattery.getGtAcbGroupTotalCapacity() == 0)
                            && gtBattery.getGtAcbGroupTotalPreCapacity() == 0) {
                        environment.setBatteryLevel(BizConstants.UNKNOWN_VALUE);
                    } else if (gtBattery.getGtAcbGroupTotalPreCapacity() > batteryLevel) {
                        batteryLevel = gtBattery.getGtAcbGroupTotalPreCapacity();
                    }
                    environment.setCheckTime(gtBattery.getCheckTime());
                    environment.setBatteryLevel(batteryLevel);
                } else {
                    environment.setBatteryLevel(BizConstants.UNKNOWN_VALUE);
                }

                if (gtAirConditionList != null && gtAirConditionList.size() > 0) {
                    GtAirCondition gtAirCondition = gtAirConditionList.get(0);
                    /* 空调 */
                    Integer airWorkStatus = gtAirCondition.getGtAirWorkStatus();
                    if (airWorkStatus == 1) {
                        environment.setAirWorkStatus(BizConstants.STATUS_RANGE_1);
                    } else if (airWorkStatus == 0) {
                        environment.setAirWorkStatus(BizConstants.STATUS_RANGE_2);
                    }
                    environment.setAirTemp(gtAirCondition.getGtAirCabinetInsideTem() / 10);
                    Integer airWorkMode = gtAirCondition.getGtAirWorkModel();
                    // 工作模式0 不制冷 1制冷 2 加热 3 制冷除湿 4 加热除湿
                    if (airWorkMode == null) {
                        environment.setAirWorkMode(-2);
                    } else if (airWorkMode == 0) {
                        environment.setAirWorkMode(3);
                    } else if (airWorkMode == 1) {
                        environment.setAirWorkMode(4);
                    } else if (airWorkMode == 2) {
                        environment.setAirWorkMode(5);
                    } else if (airWorkMode == 3) {
                        environment.setAirWorkMode(6);
                    } else if (airWorkMode == 4) {
                        environment.setAirWorkMode(7);
                    } else {
                        environment.setAirWorkMode(-2);
                    }
                    environment.setCheckTime(gtAirCondition.getCheckTime());
                } else {
                    environment.setAirWorkStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    environment.setAirWorkMode(-2);
                    environment.setAirTemp(BizConstants.UNKNOWN_VALUE);
                }
                if (gtTempHum != null) {
                    /* 温度 */
                    environment.setTemperature(gtTempHum.getGtTemHumEquipAmbientTemp() / 10);
                    /* 湿度 */
                    environment.setHumidity(gtTempHum.getGtTemHumEquipAmbientHumidity() / 10);
                    environment.setCheckTime(gtTempHum.getCheckTime());
                } else {
                    /* 温度 */
                    environment.setTemperature(BizConstants.UNKNOWN_VALUE);
                    /* 湿度 */
                    environment.setHumidity(BizConstants.UNKNOWN_VALUE);
                }
                if (gtAlarmInfo != null) {
                    /* 烟感 */
                    if (gtAlarmInfo.getGtAlarmSmogStatus() == null) {
                        environment.setSmogStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    } else if (gtAlarmInfo.getGtAlarmSmogStatus() == 0) {
                        environment.setSmogStatus(BizConstants.STATUS_RANGE_2);
                    } else if (gtAlarmInfo.getGtAlarmSmogStatus() == 1) {
                        environment.setSmogStatus(BizConstants.STATUS_RANGE_1);
                    } else {
                        environment.setSmogStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    }
                    /* 水浸 */
                    if (gtAlarmInfo.getGtAlarmWaterLoggingStatus() == null) {
                        environment.setWaterLoggingStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    } else if (gtAlarmInfo.getGtAlarmWaterLoggingStatus() == 0) {
                        environment.setWaterLoggingStatus(BizConstants.STATUS_RANGE_2);
                    } else if (gtAlarmInfo.getGtAlarmWaterLoggingStatus() == 1) {
                        environment.setWaterLoggingStatus(BizConstants.STATUS_RANGE_1);
                    } else {
                        environment.setWaterLoggingStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    }
                    environment.setCheckTime(gtAlarmInfo.getCheckTime());
                } else {
                    /* 烟感 */
                    environment.setSmogStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    /* 水浸 */
                    environment.setWaterLoggingStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                }
            } else if ("03".equals(deviceBrandCode)) {
                FhAlarm fhAlarm = deviceMapper.getFhAlarm(gantryId);
                FhPowerInfo fhPowerInfo = deviceMapper.getFhPowerInfo(gantryId);
                List<FhBattery> fhBattery = deviceMapper.getFhBattery(gantryId);
                List<FhAirCondition> fhAirCondition = deviceMapper.getFhAirCondition(gantryId);
                FhTempHum fhTempHum = deviceMapper.getFhTempHum(gantryId);
                environment = new InfoGantryStatusEnvironment();
                if (fhAlarm != null) {
                    //烟感 0 告警 1 正常
                    if (StringUtils.isBlank(fhAlarm.getSmokeStatus())) {
                        environment.setSmogStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    } else if (fhAlarm.getSmokeStatus().equals("0")) {
                        environment.setSmogStatus(BizConstants.STATUS_RANGE_2);
                    } else if (fhAlarm.getSmokeStatus().equals("1")) {
                        environment.setSmogStatus(BizConstants.STATUS_RANGE_1);
                    } else {
                        environment.setSmogStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    }
                    // 水浸 0 正常 1 告警
                    if (StringUtils.isBlank(fhAlarm.getWaterStatus())) {
                        environment.setWaterLoggingStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    } else if (fhAlarm.getWaterStatus().equals("0")) {
                        environment.setWaterLoggingStatus(BizConstants.STATUS_RANGE_1);
                    } else if (fhAlarm.getWaterStatus().equals("1")) {
                        environment.setWaterLoggingStatus(BizConstants.STATUS_RANGE_2);
                    } else {
                        environment.setWaterLoggingStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    }
                } else {
                    environment.setSmogStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    environment.setWaterLoggingStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                }
                if (fhPowerInfo != null) {
                    // 电表电压 电压为0
                    String phaseVoltage = fhPowerInfo.getPhaseVoltage();
                    // 交流输入故障 0 正常 1 告警
                    String inputACStatus = fhPowerInfo.getInputACStatus();
                    // 输入相电压 电压为0
                    String inputPhaseVoltage = fhPowerInfo.getInputPhaseVoltage();
                    // 电池放电状态 0 正常 1 放电
                    String status = fhPowerInfo.getBatteryStatus();
                    /* 判断供电模式 如果交流输入故障正常和电池放电正常就是市电模式，如果两个都告警那就是ups供电 */
                    if (inputPhaseVoltage != null && inputPhaseVoltage.equals("0")) {
                        // 市电断电
                        environment.setPowerSupplyModeRange(BizConstants.STATUS_RANGE_2);
                        environment.setPowerSupplyMode(2);
                    } else if (inputACStatus != null && inputACStatus.equals("1")) {
                        // ups
                        environment.setPowerSupplyModeRange(BizConstants.STATUS_RANGE_2);
                        environment.setPowerSupplyMode(2);
                    } else if (inputPhaseVoltage != null && !inputPhaseVoltage.equals("0")
                            && inputACStatus != null && inputACStatus.equals("0")
                            && phaseVoltage != null && !phaseVoltage.equals("0")) {
                        environment.setPowerSupplyModeRange(BizConstants.STATUS_RANGE_1);
                        environment.setPowerSupplyMode(1);
                    } else {
                        environment.setPowerSupplyModeRange(BizConstants.STATUS_RANGE_UNKNOWN);
                        environment.setPowerSupplyMode(-2);
                    }
                    /* 输入电压 */
                    try {
                        if (StringUtils.isNotBlank(inputPhaseVoltage)) {
                            Double voltageDouble = Double.parseDouble(inputPhaseVoltage) / 100;
                            environment.setVoltage(voltageDouble.intValue());
                        } else {
                            environment.setVoltage(BizConstants.UNKNOWN_VALUE);
                        }
                    } catch (Exception ex) {
                        environment.setVoltage(BizConstants.UNKNOWN_VALUE);
                    }
                    /* 电流 */
                    String electricCurrent = fhPowerInfo.getPhaseElectricCurrent();
                    if (electricCurrent != null) {
                        Double electricCurrentDouble = Double.parseDouble(electricCurrent) / 100;
                        environment.setElectricCurrent(electricCurrentDouble.intValue());
                    } else {
                        environment.setElectricCurrent(BizConstants.UNKNOWN_VALUE);
                    }
                } else {
                    environment.setPowerSupplyModeRange(BizConstants.STATUS_RANGE_UNKNOWN);
                    environment.setPowerSupplyMode(-2);
                    environment.setVoltage(BizConstants.UNKNOWN_VALUE);
                    environment.setElectricCurrent(BizConstants.UNKNOWN_VALUE);
                }
                if (fhBattery != null && fhBattery.size() > 0) {
                    /* 电池电量，现在电池剩余电量不准确，电池组可以自己计算百分比 */
                    Integer batteryLevel = BizConstants.UNKNOWN_VALUE;
                    FhBattery fhBattery1 = fhBattery.get(0);
                    FhBattery fhBattery2 = fhBattery.get(1);
                    String bmsTotalCapacity = fhBattery1.getBmsTotalCapacity();
                    String bmsTotalCapacity1 = fhBattery2.getBmsTotalCapacity();
                    if (bmsTotalCapacity == null || bmsTotalCapacity1 == null) {
                        environment.setBatteryLevel(BizConstants.UNKNOWN_VALUE);
                    } else {
                        float t1 = Integer.parseInt(bmsTotalCapacity) == 0 ? 0 : 100;
                        float t2 = Integer.parseInt(bmsTotalCapacity1) == 0 ? 0 : 100;
                        /* 如电池容量未获取到且剩余百分比为0，视为信息异常 */
                        if ((fhBattery1.getTotalCapacity() == null || fhBattery1.getTotalCapacity().equals("0"))
                                && fhBattery1.getRemainCapacity().equals("0")) {
                            environment.setBatteryLevel(BizConstants.UNKNOWN_VALUE);
                            logger.error("烽火电池剩余容量异常！！！");
                        }
                        if ((t1 + t2) > 0) {
                            // 电池剩余量 1
                            String bmsRemainCapacity = fhBattery1.getBmsRemainCapacity();
                            // 电池剩余量 2
                            String bmsRemainCapacity1 = fhBattery2.getBmsRemainCapacity();
                            float i = Integer.parseInt(bmsRemainCapacity) + Integer.parseInt(bmsRemainCapacity1);
                            float totalCapacity = (t1 + t2) * 100;
                            batteryLevel = (int) (i / totalCapacity * 100);
                        } else {
                            environment.setBatteryLevel(BizConstants.UNKNOWN_VALUE);
                            logger.error("烽火电池两个电池组都没有数据！！！");
                        }
                        environment.setCheckTime(fhBattery1.getCheckTime());
                        environment.setBatteryLevel(batteryLevel);
                    }
                } else {
                    logger.error("烽火电池没有数据！！！");
                    environment.setBatteryLevel(BizConstants.UNKNOWN_VALUE);
                }
                if (fhAirCondition != null && fhAirCondition.size() > 0) {
                    FhAirCondition fhAirCondition1 = fhAirCondition.get(0);
                    // 工作状态 1待机 2 运行 3 故障
                    String workStatus = fhAirCondition1.getWorkStatus();
                    // 烽火没有工作模式
                    String airTemp = fhAirCondition1.getAirTemp();
                    if (workStatus.equals("1")) {
                        environment.setAirWorkStatus(BizConstants.STATUS_RANGE_1);
                    } else if (workStatus.equals("2")) {
                        environment.setAirWorkStatus(BizConstants.STATUS_RANGE_1);
                    } else if (workStatus.equals("3")) {
                        environment.setAirWorkStatus(BizConstants.STATUS_RANGE_2);
                    } else {
                        environment.setAirWorkStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    }
                    environment.setAirTemp(Integer.parseInt(airTemp));
                    environment.setAirWorkMode(3);
                } else {
                    environment.setAirWorkStatus(BizConstants.STATUS_RANGE_UNKNOWN);
                    environment.setAirWorkMode(-2);
                    environment.setAirTemp(BizConstants.UNKNOWN_VALUE);
                }
                if (fhTempHum != null) {
                    /* 温度 */
                    environment.setTemperature(Integer.parseInt(fhTempHum.getTemperature()) / 10);
                    /* 湿度 */
                    environment.setHumidity(Integer.parseInt(fhTempHum.getHumidity()) / 10);
                    environment.setCheckTime(fhTempHum.getCheckTime());
                } else {
                    /* 温度 */
                    environment.setTemperature(BizConstants.UNKNOWN_VALUE);
                    /* 湿度 */
                    environment.setHumidity(BizConstants.UNKNOWN_VALUE);
                }
            }
        }
        return environment;
    }
}
